package com.pn.controller;

import com.pn.entity.CurrentUser;
import com.pn.entity.OutStore;
import com.pn.entity.Result;
import com.pn.entity.Store;
import com.pn.page.Page;
import com.pn.service.OutStoreService;
import com.pn.service.StoreService;
import com.pn.utils.TokenUtils;
import com.pn.utils.WarehouseConstants;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/outstore")
public class OutStoreController {

    @Resource
    private OutStoreService outStoreService;
    @Resource
    private TokenUtils tokenUtils;
    @Resource
    private StoreService storeService;

    //添加出库单
    @RequestMapping("/outstore-add")
    public Result addOutStore(@RequestBody OutStore outStore, @RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String token){
        CurrentUser currentUser = tokenUtils.getCurrentUser(token);
        int userId = currentUser.getUserId();
        outStore.setCreateBy(userId);
        int result = outStoreService.addOutStore(outStore);
        if (result > 0){
            return Result.ok("出库单添加成功！");
        }
        return Result.err(Result.CODE_ERR_BUSINESS,"出库单添加失败！");
    }

    //查询所有仓库
    @RequestMapping("/store-list")
    public Result findAllStore(){
        List<Store> allStore = storeService.findAllStore();
        return Result.ok(allStore);
    }

    //分页查询所有出库单记录
    @RequestMapping("/outstore-page-list")
    public Result findOutStoreByPage(OutStore outStore, Page page){
        Page outStoreByPage = outStoreService.findOutStoreByPage(outStore, page);
        return Result.ok(outStoreByPage);
    }

    //确认出库
    @RequestMapping("/outstore-confirm")
    public Result confirmOutStore(@RequestBody OutStore outStore,@RequestHeader(value = WarehouseConstants.HEADER_TOKEN_NAME) String token){
        CurrentUser currentUser = tokenUtils.getCurrentUser(token);
        int userId = currentUser.getUserId();
        Result result = outStoreService.modifyIsOut(outStore, userId);
        return result;
    }
}
